﻿using Swsk33.BasicGISGDI.Model;
using Swsk33.BasicGISGDI.Util;
using System.Drawing;
using System.Windows.Forms;

namespace TestDllWinForm
{
	public partial class MainGUI : Form
	{
		/// <summary>
		/// 当前画的点
		/// </summary>
		private Point[] points = new Point[3];

		/// <summary>
		/// 当前点索引
		/// </summary>
		private int currentIndex = 0;

		/// <summary>
		/// 是否正在绘制
		/// </summary>
		private bool drawing = false;

		/// <summary>
		/// 绘制器
		/// </summary>
		private ElementPainter painter;

		public MainGUI()
		{
			InitializeComponent();
			painter = new ElementPainter(showArea.CreateGraphics());
		}

		private void showArea_MouseClick(object sender, MouseEventArgs e)
		{
			if (drawing)
			{
				// 绘制点
				points[currentIndex] = new Point(e.X, e.Y);
				painter.drawPoint(points[currentIndex]);
				if (currentIndex == 1)
				{
					LineSegment segment = new LineSegment(points[0], points[1]);
					painter.drawLine(segment);
				}
				currentIndex++;
				if (currentIndex == points.Length)
				{
					LineSegment line = new LineSegment(points[0], points[1]);
					result.Text = line.GetPointToLineShortestDistance(points[2]) + "";
					drawing = false;
					ok.Enabled = true;
					currentIndex = 0;
				}
			}
		}

		private void ok_Click(object sender, System.EventArgs e)
		{
			showArea.Refresh();
			drawing = true;
			ok.Enabled = false;
		}
	}
}
